package controller;

import com.alibaba.fastjson.JSON;
import entity.Userinfo;
import service.serviceimpl.ServiceUserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2023/5/23 10:25
 * @PackageName:${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
@WebServlet(name = "ServletTest3", urlPatterns = "/ServletTest3")
public class ServletTest3 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        String op = request.getParameter("op");
        if (op != null) {
            ServiceUserImpl serviceUser = new ServiceUserImpl();
            if (op.equals("load")) {
                showlist(request, response);
            } else if (op.equals("delete")) {
                int id = Integer.parseInt(request.getParameter("id"));
                int i = serviceUser.executeUpdate("delete from userinfo where id=?", id);
                if (i > 0) {
                    request.setAttribute("delete", "删除成功");
                    showlist(request, response);
                } else {
                    request.setAttribute("delete", "删除失败");
                    showlist(request, response);
                }
            } else if (op.equals("delete1")) {
                int id = Integer.parseInt(request.getParameter("id"));
                int i = serviceUser.executeUpdate("delete from userinfo where id=?", id);
                if (i > 0) {
                  out.print("y");
                } else {
                    out.print("n");
                }
            }else if (op.equals("delete2")) {
                int id = Integer.parseInt(request.getParameter("id"));
                //数据库删除了
                int i=  serviceUser.executeUpdate("delete from userinfo where id=?",id);
                if(i>0){
                    out.print(JSON.toJSONString("y"));
                }else{
                    out.print(JSON.toJSONString("n"));
                }
            }


            else if (op.equals("update1")) {
                //拿到传入要修改的id
                int id = Integer.parseInt(request.getParameter("id"));
                request.setAttribute("id", id);
                List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo where id=?", id);
                request.setAttribute("list", userinfos);
                request.getRequestDispatcher("/jstlnc/update.jsp").forward(request, response);
            } else if (op.equals("update2")) {
                int id = Integer.parseInt(request.getParameter("id"));
                String name = request.getParameter("name");
                String dept = request.getParameter("dept");
                int sex = Integer.parseInt(request.getParameter("sex"));
                int i = serviceUser.executeUpdate("update userinfo set name=?,sex=?,dept=? where id=?", name, sex, dept, id);
                if (i > 0) {//修改成功
                    request.setAttribute("update", "修改成功");
                    showlist(request, response);
                } else {//修改失败
                    request.setAttribute("update", "修改失败");
                    showlist(request, response);
                }

            } else if (op.equals("add")) {
                //着手增加
                String name = request.getParameter("name");
                String dept = request.getParameter("dept");
                int sex = Integer.parseInt(request.getParameter("sex"));
                int i = serviceUser.executeUpdate("insert into userinfo(name,sex,age,ranking,dept) values(?,?,18,'1',?) "
                        , name, sex, dept);
                if (i > 0) {//增加成功
                    request.setAttribute("add", "增加成功");
                    showlist(request, response);
                } else {//增加失败
                    request.setAttribute("add", "增加失败");
                    showlist(request, response);
                }
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/jstlnc/load.jsp");
           // showlist(request, response);
        }
        out.flush();
        out.close();
    }
    //封装一个带list的方法

    private static void showlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo");
        request.setAttribute("list", userinfos);
        request.getRequestDispatcher("/jstlnc/main.jsp").forward(request, response);
    }

    //除了登录页面，前台页面都要先经过中转页--
    //jstl 必须要从中转页request.setAttribute(); ${}
    //不管操作成功与否，只要过了servlet就必须重新拿最新的list 回去


}
